var serverHost = "http://192.168.2.100";
serverHost = "http://47.98.167.243";
// serverHost = "http://127.0.0.1";
var serverPort = "8100";
// serverPort = "8080";
var projectPath = "/blp-app/";
var serverPath = serverHost + ":" + serverPort + "/";
var web_url_prefix = serverHost + ":" + serverPort + projectPath;


var remoteHost = "http://47.98.167.243";
var picPath = serverHost + ":" + serverPort;
var web_login_url = serverPath + "blp/login.html";



var upmsPath = "http://upms.jites.com.cn/account/";
//  upmsPath = "http://dev02.jites.com.cn/account/";
var userInfo = getStorJson("userInfo");



// blp管理平台的appKey
var appKey = "98fecdf0-ea40-11e8-8eb6-00163e08a47e";
// 安卓应用的appKey
var androidAppKey = "68de7c81-eaec-11e8-8eb6-00163e08a47e";

//设置字符串类型的本地缓存
function setStorage(objName, objValue) {
    var sto = window.localStorage;
    if (sto)
        sto.setItem(objName, objValue);
}
//读取字符串类型的本地缓存
function getStorage(objName) {
    var ret = '';
    var sto = window.localStorage;
    if (sto)
        ret = sto.getItem(objName);
    return ret;
}
//清除本地缓存，如没指定名称则为清空所有缓存
function clearStorage(objName) {
    var sto = window.localStorage;
    if (sto) {
        if (objName)
            sto.removeItem(objName);
        else
            sto.clear();
    }
}
//设置Json类型的本地缓存
function setStorJson(objName, json) {
    if (json)
        setStorage(objName, JSON.stringify(json));
}
//读取Json类型的本地缓存
function getStorJson(objName) {
    var ret = null;
    var str = getStorage(objName);
    if (str)
        ret = JSON.parse(str);
    return ret;
}

function ajax(opt) {
    opt.data.from = "pc";
    // var userInfo = getStorJson("userInfo");
    // if (userInfo) {
    //     opt.data.current_login_access_token = userInfo.userMap.accessToken;
    //     opt.data.current_login_uid = userInfo.user.user_id;
    // }
    var defaultOpt = {
        url: "",
        type: "GET",
        dataType: 'jsonp',
        jsonp: "callback",
        data: {},
        timeout: 10000, //超时时间
        error: function (xhr, type) {}
    };
    opt.url = web_url_prefix + opt.url;
    opt = extend(defaultOpt, opt);
    if (opt.loading) {
        opt.beforeSend = function (xhr, settings) {
            //openLoading();
        };
    }
    if (opt.successx) {
        opt.success = function (data, status) {
            console.log(data)
            if (data && data.success == false) { //针对授权过期统一处理
                if(data && data.code == 60000){
                    //跳转登陆
                    // location.href=data.data.href+"?redirect_uri=" + location.href;
                    location.href = "login.html" + "?redirect_uri=" + location.href;
                } else if (data && data.code == 60403) {
                    location.href = "403.html";
                }
            }
            opt.successx(data);
        };
    }
    $.ajax(opt);
}

//ajax post
function ajaxPost(opt) {
    opt.data.from = "pc";
    var defaultOpt = {
        url: "",
        type: "POST",
        dataType: 'json',
        data: {},
        timeout: 10000, //超时时间
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        error: function (xhr, type) {}
    };
    opt.url = web_url_prefix + opt.url;
    opt = extend(defaultOpt, opt);
    if (opt.loading) {
        opt.beforeSend = function (xhr, settings) {
            //openLoading();
        };
    }
    if (opt.successx) {
        opt.success = function (data, status) {
            if (data && data.success == false) { //针对授权过期统一处理
                if(data&& data.code == 60000){
                    //跳转登陆
                    location.href=data.data.href+"?redirect_uri=" + location.href;
                }
            }
            opt.successx(data);
        };
    }
    $.ajax(opt);
}

function extend(target, source) {
    for (var p in source) {
        if (source.hasOwnProperty(p)) {
            target[p] = source[p];
        }
    }
    return target;
};

//判断图片路径,返回绝对路径
function changeImgUrl(str) {
    if (str == null || str == "" || str == undefined || str == "undefined" || str.replace(/(^s*)|(s*$)/g, "").length == 0 || str == "null") {
        return str
    } else if (str.indexOf("http") >= 0) {
        return str
    } else {
        str = picPath + str
        return str
    }
}

//suming tip dialog

var SUM = {};
SUM.Loading = $.Loading = function (opts) {
    var settings = {
        info: "加载中"
    };
    opts = $.extend(settings, opts);
    var tpl = '<div class="popup-box empty-box popup-sta" style="display: -webkit-box;">' +
        '    <div class="load-sta"><i class="load-icon"></i>' +
        '        <p>' + opts.info + '</p>'
    '    </div>' +
    '</div>';
    $(document).ajaxStart(function () {
        if ($(".popup-sta").length === 0) {
            //$(tpl).appendTo($(document.body));
            parent.layer.load(2, {time: 30*1000});
        }
        // setTimeout(function () {
        //     $(".popup-sta").remove();
        // }, 60000)
    }).ajaxStop(function () {
        $(".popup-sta").remove();
        parent.layer.closeAll('loading');
    })

}
SUM.Loading({
    info: "请稍后"
})


//登录
function setWinParam(param) {
    localStorage.winParam = null;
    localStorage.winParam = JSON.stringify(param);
}

function getWinParam() {
    var p = null;
    if (localStorage.winParam) {
        p = JSON.parse(localStorage.winParam);
        localStorage.winParam = null;
    }
    return p;
}

/**
 *判断用户有没登录
 */
function goLoginHtml(callback) {
    var userInfo = getStorJson("userInfo");
    if (userInfo == "" || userInfo == null) {
        location.href = "login.html";
    } else {
        callback && callback();
    }
}

/*只判断是否存在用户，不登录跳转,返回用户信息 */
function isLoginB(callback) {
    var userInfo = getStorJson("userInfo");
    if (userInfo == "" || userInfo == null) {
        return false
    } else {
        callback && callback(userInfo);
    }
}

function unLogin(callback) {

    var userInfo = getStorJson("userInfo");

    if (userInfo == "" || userInfo == null) {

    } else {

        callback && callback(userInfo);
    }
}

SUM.Loading({
    info: "请稍后"
})


//监听enter键
function keyEnter(d) {
    window.onkeydown = function () {
        if (event.keyCode == 13) {
            $("input").blur();
            d();
        }
    }
};

//时间处理-时间轴=>年月日格式
function timeStamp2String(time, id) {
    if (time == null || time == undefined || time == "undefined") return "";
    //y=年;ymd=年月日;ymdhm年月日时分;ymdhms年月日时分秒
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var month2 = datetime.getMonth() + 1 < 10 ? datetime.getMonth() + 1 : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var date2 = datetime.getDate() < 10 ? datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    var dataText = [{
        "month": "January",
        "abbr": "Jan",
        "Chinese": "一月",
        "num": "01"
    },
        {
            "month": "February",
            "abbr": "Feb",
            "Chinese": "二月",
            "num": "02"
        },
        {
            "month": "March",
            "abbr": "Mar",
            "Chinese": "三月",
            "num": "03"
        },
        {
            "month": "April",
            "abbr": "Apr",
            "Chinese": "四月",
            "num": "04"
        },
        {
            "month": "May",
            "abbr": "May",
            "Chinese": "五月",
            "num": "05"
        },
        {
            "month": "June",
            "abbr": "Jun",
            "Chinese": "六月",
            "num": "06"
        },
        {
            "month": "July",
            "abbr": "Jul",
            "Chinese": "七月",
            "num": "07"
        },
        {
            "month": "August",
            "abbr": "Aug",
            "Chinese": "八月",
            "num": "08"
        },
        {
            "month": "September",
            "abbr": "Sep",
            "Chinese": "九月",
            "num": "09"
        },
        {
            "month": "October",
            "abbr": "Oct",
            "Chinese": "十月",
            "num": "10"
        },
        {
            "month": "November",
            "abbr": "Nov",
            "Chinese": "十一月",
            "num": "11"
        },
        {
            "month": "December",
            "abbr": "Dec",
            "Chinese": "十二月",
            "num": "12"
        }
    ];
    if (id == "y") {
        return year;
    } else if (id == "ym") {
        return year + "-" + month;
    } else if (id == "ymd") {
        return year + "-" + month + "-" + date;
    } else if (id == "ymd2") {
        return year + "/" + month + "/" + date;
    } else if (id == "ymd3") {
        return year + "." + month + "." + date;
    } else if (id == "ymdhm") {
        return year + "-" + month + "-" + date + " " + hour + ":" + minute;
    } else if (id == "ymdhms") {
        return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
    } else if (id == "md") {
        return month + "-" + date;
    } else if (id == "hm") {
        return hour + ":" + minute;
    } else if (id == "md2") {
        return month2 + "月" + date2 + "日";
    } else if (!id) {
        return year + "-" + month + "-" + date;
    } else if (id == "mdhm") {
        return month + "-" + date + " " + hour + ":" + minute;
    } else if (id == "md3") {
        return dataText[datetime.getMonth()].abbr + " " + date;
    } else if (id == "hms") {
        return hour + ":" + minute + ":" + second;
    }
}


//把字符串日期转为日期
function convertStrToDate(datetimeStr) {
    var mydateint = Date.parse(datetimeStr);//数值格式的时间
    if (!isNaN(mydateint)) {
        var mydate = new Date(mydateint);
        return mydate;
    }
    var mydate = new Date(datetimeStr);//字符串格式时间
    var monthstr = mydate.getMonth() + 1;
    if (!isNaN(monthstr)) {//转化成功
        return mydate;
    }//字符串格式时间转化失败
    var dateParts = datetimeStr.split(" ");
    var dateToday = new Date();
    var year = dateToday.getFullYear();
    var month = dateToday.getMonth();
    var day = dateToday.getDate();
    if (dateParts.length >= 1) {
        var dataPart = dateParts[0].split("-");//yyyy-mm-dd  格式时间
        if (dataPart.length == 1) {
            dataPart = dateParts[0].split("/");//yyyy/mm/dd格式时间
        }
        if (dataPart.length == 3) {
            year = Math.floor(dataPart[0]);
            month = Math.floor(dataPart[1]) - 1;
            day = Math.floor(dataPart[2]);
        }
    }
    if (dateParts.length == 2) {//hh:mm:ss格式时间
        var timePart = dateParts[1].split(":");//hh:mm:ss格式时间
        if (timePart.length == 3) {
            var hour = Math.floor(timePart[0]);
            var minute = Math.floor(timePart[1]);
            var second = Math.floor(timePart[2]);
            return new Date(year, month, day, hour, minute, second);
        }
    }
    else {
        return new Date(year, month, day);
    }
}


/** 判断对象是否为空 **/
function isNullObject(map) {
    if (typeof map === "object" && !(map instanceof Array)) {
        var hasProp = true;
        for (var prop in map) {
            hasProp = false;
            break;
        }
        return hasProp
    }
}
/** 判断字符串是否为空 **/
function isNULL(str) {
    if (str == null || str == "" || str == undefined || str == "undefined" || str.replace(/(^s*)|(s*$)/g, "").length == 0 || str == "null") {
        return true;
    }
    return false;
}

/** 判断字符串是否为空 **/
function setNull(str) {
    if (str == null || str == "" || str == undefined || str == "undefined" || str.replace(/(^s*)|(s*$)/g, "").length == 0 || str == "null") {
        return "";
    }
    return str;
}

/**
 * 获取地址栏后参数
 */
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
/* 导出excel */
$(".exportExcel").on("click", function () {
    var filename = $(".page-heading h2").text()
    $(".table2excel").table2excel({
        exclude: ".noExl",
        name: "Excel Document Name",
        filename: filename + timeStamp2String(Date.parse(new Date()), "ymdhms"),
        fileext: ".xls",
        exclude_img: true,
        exclude_links: true,
        exclude_inputs: true
    });
})

/*
 * 获取垂直滚动条宽度
 */
function getScrollWidth() {
    var noScroll, scroll, oDiv = document.createElement("DIV");
    oDiv.style.cssText = "position:absolute; top:-1000px; width:100px; height:100px; overflow:hidden;";
    noScroll = document.body.appendChild(oDiv).clientWidth;
    oDiv.style.overflowY = "scroll";
    scroll = oDiv.clientWidth;
    document.body.removeChild(oDiv);


    return noScroll - scroll;
}
//点击查看图片
function previewImg() {
    $(".previewImg").click(function () {
        var tagName = $(this)[0].tagName
        if (tagName == "IMG") {
            var previewImgpath = $(this).attr("src");
            var previewImgpathList = [];
            var start = 0;
            var previewImgpathObj = {}
            $(".previewImg").each(function (i, d) {
                if ($(d).attr("src") == previewImgpath) {
                    start = i
                }
                previewImgpathObj = {
                    "src": $(d).attr("src")
                }
                previewImgpathList.push(previewImgpathObj)
            })
            var photoJson = {
                "start": start,
                "data": previewImgpathList
            }
            layer.photos({
                photos: photoJson
            });
        }
    })
}

$(".previewImg").click(function () {
    var tagName = $(this)[0].tagName
    if (tagName == "IMG") {
        var previewImgpath = $(this).attr("src");
        var previewImgpathList = [];
        var start = 0;
        var previewImgpathObj = {}
        $(".previewImg").each(function (i, d) {
            if ($(d).attr("src") == previewImgpath) {
                start = i
            }
            previewImgpathObj = {
                "src": $(d).attr("src")
            }
            previewImgpathList.push(previewImgpathObj)
        })

        var photoJson = {
            "start": start,
            "data": previewImgpathList
        }
        layer.photos({
            photos: photoJson
        });
    }
})
/**
 *undefined重置为空
 */
function undeToNull(str){
    if(str == null || str == "" || str == undefined || str == "undefined"){
        str = "";
    }
    return str;
}

function backForwardListen() {
    history.go(-1);
}
